﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Kingdee.BOS.Core.Enums.BOSEnums;

namespace RK.ReportPlugin.BPW
{
    [Description("BPW_简单账表：凸轮轴计划报表"), HotUpdate]
    public class BpwTuLunReport : SysReportBaseService
    {
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.ReportProperty.ReportName = new LocaleValue("凸轮轴计划报表", base.Context.UserLocale.LCID);
            this.IsCreateTempTableByPlugin = true;
            this.ReportProperty.IsUIDesignerColumns = false;
            this.ReportProperty.IsGroupSummary = true;
            this.ReportProperty.SimpleAllCols = false;
            this.ReportProperty.PrimaryKeyFieldName = "CPTH";
            this.ReportProperty.IdentityFieldName = "FIDENTITYID";
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            List<SqlParam> sqlParams = new List<SqlParam>();
            sqlParams.Add(new SqlParam("@TempName", KDDbType.AnsiString, tableName));
            DBUtils.ExecuteStoreProcedure(this.Context, "P_ZSLH_TuLunReport", sqlParams);
        }
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            var lastDate = DateTime.Now.AddMonths(-1).AddDays(1 - DateTime.Now.AddMonths(-1).Day).ToString("d");
            var curDate = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("d");
            var net1StarDate = DateTime.Now.AddMonths(1).AddDays(1 - DateTime.Now.AddMonths(1).Day).ToString("d");
            var net2StarDate = DateTime.Now.AddMonths(2).AddDays(1 - DateTime.Now.AddMonths(1).Day).ToString("d");
            var net3StarDate = DateTime.Now.AddMonths(3).AddDays(1 - DateTime.Now.AddMonths(1).Day).ToString("d");
            var endDate = DateTime.Now.AddMonths(3).AddDays(1 - DateTime.Now.AddMonths(3).Day).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59.999");

            string sql0 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{lastDate}','{curDate}'");
            var ds0 = DBServiceHelper.ExecuteDataSet(this.Context, sql0);
            string sql1 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{curDate}','{net1StarDate}'");
            var ds1 = DBServiceHelper.ExecuteDataSet(this.Context, sql1);
            string sql2 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{net1StarDate}','{net2StarDate}'");
            var ds2 = DBServiceHelper.ExecuteDataSet(this.Context, sql2);
            string sql3 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{net2StarDate}','{net3StarDate}'");
            var ds3 = DBServiceHelper.ExecuteDataSet(this.Context, sql3);
            string sql4 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{net3StarDate}','{endDate}'");
            var ds4 = DBServiceHelper.ExecuteDataSet(this.Context, sql4);

            var year = DateTime.Now.Year.ToString();
            var lastMonth = DateTime.Now.AddMonths(-1).Month.ToString();
            var month = DateTime.Now.Month.ToString();
            var net1Month = (DateTime.Now.AddMonths(1)).Month.ToString();
            var net2Month = (DateTime.Now.AddMonths(2)).Month.ToString();
            var net3Month = (DateTime.Now.AddMonths(3)).Month.ToString();
            ReportHeader header = new ReportHeader();
            //header.AddChild("FIDENTITYID", new LocaleValue("序号"), SqlStorageType.SqlInt, visible: true);
            header.AddChild("AGS型号", new LocaleValue("规格"), SqlStorageType.Sqlnvarchar, visible: true);
            //header.AddChild("MATNAME", new LocaleValue("品名"), SqlStorageType.Sqlnvarchar, visible: true);
            header.AddChild("CPTH", new LocaleValue("成品图号"), SqlStorageType.Sqlnvarchar, visible: true);
            header.AddChild("MPTH", new LocaleValue("毛坯图号"), SqlStorageType.Sqlnvarchar, visible: true);
            header.AddChild("QTTH", new LocaleValue("其他图号"), SqlStorageType.Sqlnvarchar, visible: true);

            header.AddChild("材料仓", new LocaleValue("材料仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("待检仓", new LocaleValue("待检仓总数", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("庆达待检仓", new LocaleValue("庆达供应商待检数量", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("不良品仓", new LocaleValue("不良品仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("积压配件仓", new LocaleValue("积压配件仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("售后仓", new LocaleValue("售后仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("配件仓", new LocaleValue("配件仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("总装车间仓", new LocaleValue("总装车间仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("机加工车间仓", new LocaleValue("机加工车间仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("电泳车间仓", new LocaleValue("电泳车间仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("嘉资仓", new LocaleValue("嘉资仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("车间委外仓", new LocaleValue("车间委外仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("庆达仓", new LocaleValue("庆达仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("JV成品数", new LocaleValue("不含待检在制总数", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("AllQty", new LocaleValue("含待检在制总数", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("当年预测", new LocaleValue($"{year}年预测", 2052), SqlStorageType.SqlInt, visible: true);
            header.AddChild("出口仓", new LocaleValue("出口仓", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("FSAFESTOCK", new LocaleValue("建议安全库存", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("订单合计", new LocaleValue("订单合计", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("ZXZS2", new LocaleValue("安全库存尚需总数量", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("ZXZS", new LocaleValue("订单欠发尚需总数量", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("DYJS", new LocaleValue("当月欠交数量", 2052), SqlStorageType.SqlDecimal);
            header.AddChild("上月订单数量", new LocaleValue($"{lastMonth}月订单数量", 2052), SqlStorageType.SqlDecimal);
            if (ds0.Tables.Count > 0)
            {
                if (ds0.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds0.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds0.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            var m = Convert.ToInt32(colName.Substring(5, 2));
                            var d = Convert.ToInt32(colName.Substring(7, 2));
                            header.AddChild(colName, new LocaleValue($"{m}月{d}日"), SqlStorageType.SqlDecimal);
                        }
                    }
                }
            }
            header.AddChild("当月订单数量", new LocaleValue($"{month}月订单数量", 2052), SqlStorageType.SqlDecimal);
            if (ds1.Tables.Count > 0)
            {
                if (ds1.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds1.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            var m = Convert.ToInt32(colName.Substring(5, 2));
                            var d = Convert.ToInt32(colName.Substring(7, 2));
                            header.AddChild(colName, new LocaleValue($"{m}月{d}日"), SqlStorageType.SqlDecimal);
                        }
                    }
                }
            }
            header.AddChild("下1月订单数量", new LocaleValue($"{net1Month}月订单数量", 2052), SqlStorageType.SqlDecimal);
            if (ds2.Tables.Count > 0)
            {
                if (ds2.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds2.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds2.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            var m = Convert.ToInt32(colName.Substring(5, 2));
                            var d = Convert.ToInt32(colName.Substring(7, 2));
                            header.AddChild(colName, new LocaleValue($"{m}月{d}日"), SqlStorageType.SqlDecimal);
                        }
                    }
                }

            }
            header.AddChild("下2月订单数量", new LocaleValue($"{net2Month}月订单数量", 2052), SqlStorageType.SqlDecimal);
            if (ds3.Tables.Count > 0)
            {
                if (ds3.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds3.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds3.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            var m = Convert.ToInt32(colName.Substring(5, 2));
                            var d = Convert.ToInt32(colName.Substring(7, 2));
                            header.AddChild(colName, new LocaleValue($"{m}月{d}日"), SqlStorageType.SqlDecimal);
                        }
                    }
                }

            }
            header.AddChild("下3月订单数量", new LocaleValue($"{net3Month}月订单数量", 2052), SqlStorageType.SqlDecimal);
            if (ds4.Tables.Count > 0)
            {
                if (ds4.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds4.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds4.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            var m = Convert.ToInt32(colName.Substring(5, 2));
                            var d = Convert.ToInt32(colName.Substring(7, 2));
                            header.AddChild(colName, new LocaleValue($"{m}月{d}日"), SqlStorageType.SqlDecimal);
                        }
                    }
                }
            }
            return header;
        }
        public override List<Kingdee.BOS.Core.Report.SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var lastDate = DateTime.Now.AddMonths(-1).AddDays(1 - DateTime.Now.AddMonths(-1).Day).ToString("d");
            var curDate = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("d");
            var net1StarDate = DateTime.Now.AddMonths(1).AddDays(1 - DateTime.Now.AddMonths(1).Day).ToString("d");
            var net2StarDate = DateTime.Now.AddMonths(2).AddDays(1 - DateTime.Now.AddMonths(1).Day).ToString("d");
            var net3StarDate = DateTime.Now.AddMonths(3).AddDays(1 - DateTime.Now.AddMonths(1).Day).ToString("d");
            var endDate = DateTime.Now.AddMonths(3).AddDays(1 - DateTime.Now.AddMonths(3).Day).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59.999");

            string sql0 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{lastDate}','{curDate}'");
            var ds0 = DBServiceHelper.ExecuteDataSet(this.Context, sql0);
            string sql1 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{curDate}','{net1StarDate}'");
            var ds1 = DBServiceHelper.ExecuteDataSet(this.Context, sql1);
            string sql2 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{net1StarDate}','{net2StarDate}'");
            var ds2 = DBServiceHelper.ExecuteDataSet(this.Context, sql2);
            string sql3 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{net2StarDate}','{net3StarDate}'");
            var ds3 = DBServiceHelper.ExecuteDataSet(this.Context, sql3);
            string sql4 = string.Format($"EXEC P_ZSLH_TuLunReport_01 '{net3StarDate}','{endDate}'");
            var ds4 = DBServiceHelper.ExecuteDataSet(this.Context, sql4);

            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("JV成品数", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("订单合计", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("上月订单数量", Enu_SummaryType.SUM));

            result.Add(new Kingdee.BOS.Core.Report.SummaryField("ZXZS", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("ZXZS2", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("DYJS", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("FSAFESTOCK", Enu_SummaryType.SUM));

            result.Add(new Kingdee.BOS.Core.Report.SummaryField("庆达待检仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("配件仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("材料仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("总装车间仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("机加工车间仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("庆达仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("嘉资仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("车间委外仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("不良品仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("电泳车间仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("待检仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("售后仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("出口仓", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("积压配件仓", Enu_SummaryType.SUM));

            result.Add(new Kingdee.BOS.Core.Report.SummaryField("当年预测", Enu_SummaryType.SUM));
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("AllQty", Enu_SummaryType.SUM));

            if (ds0.Tables.Count > 0)
            {
                if (ds0.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds0.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds0.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            result.Add(new Kingdee.BOS.Core.Report.SummaryField(colName, Enu_SummaryType.SUM));
                        }
                    }
                }
            }
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("当月订单数量", Enu_SummaryType.SUM));
            if (ds1.Tables.Count > 0)
            {
                if (ds1.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds1.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            result.Add(new Kingdee.BOS.Core.Report.SummaryField(colName, Enu_SummaryType.SUM));
                        }
                    }
                }
            }
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("下1月订单数量", Enu_SummaryType.SUM));
            if (ds2.Tables.Count > 0)
            {
                if (ds2.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds2.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds2.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            result.Add(new Kingdee.BOS.Core.Report.SummaryField(colName, Enu_SummaryType.SUM));
                        }
                    }
                }

            }
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("下2月订单数量", Enu_SummaryType.SUM));
            if (ds3.Tables.Count > 0)
            {
                if (ds3.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds3.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds3.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            result.Add(new Kingdee.BOS.Core.Report.SummaryField(colName, Enu_SummaryType.SUM));
                        }
                    }
                }

            }
            result.Add(new Kingdee.BOS.Core.Report.SummaryField("下3月订单数量", Enu_SummaryType.SUM));
            if (ds4.Tables.Count > 0)
            {
                if (ds4.Tables[0].Columns.Count > 0)
                {
                    for (int i = 0; i < ds4.Tables[0].Columns.Count; i++)
                    {
                        var colName = ds4.Tables[0].Columns[i].ColumnName;
                        if (colName != "F0")
                        {
                            result.Add(new Kingdee.BOS.Core.Report.SummaryField(colName, Enu_SummaryType.SUM));
                        }
                    }
                }
            }
            return result;
        }
    }
}
